package com.ruoyi.app.controller;

import com.ruoyi.app.domain.MemberUser;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.jwt.JwtHelper;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/app-api/auth/")
@ResponseBody
public class ApiAuthController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @PostMapping("/refresh-token")
    @ResponseBody
    public AjaxResult refreshToken(@RequestBody MemberUser member) {

        Map<String, Object> result = new HashMap<String, Object>();

//        MemberUser memberUser = memberUserService.checkLogin(member.getUsername());
//        if(memberUser == null){
//            return error("帐号或密码错误");
//        }
//        if(!SecurityUtils.matchesPassword(member.getPassword(),memberUser.getPassword())){
//            return error("帐号或密码错误");
//        }
//        String jwtToken = JwtHelper.generateToken("username", memberUser.getUsername());
//
//        result.put("token", jwtToken);
//
//        // 登录成功后的公共逻辑
//        // 更新用户最近登录时间
//        MemberUser memberCom = new MemberUser();
//        memberCom.setId(memberUser.getId());
//        memberCom.setLoginIp(IpUtils.getIpAddr());
//        memberCom.setLoginDate(new Date());
//        memberUserService.updateMemberUser(memberCom);

        return AjaxResult.success("成功",result);
    }
}
